package cz.spock.cms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cz.spock.cms.entity.CSS;
import cz.spock.cms.service.CSSService;
import cz.spock.core.controller.BaseController;

/**
 * serves all css related requests
 * 
 * TODO remake getCssFile method for common use as getFile to serve all static requests
 *
 * TODO handle errors/exceptions
 * 
 * @author miso
 *
 */
@Controller
public class CssController extends BaseController {
    
    @Autowired
    private CSSService cService;
    
    /**
     * sends css requested file
     * 
     * @param request
     * @param response
     */
    @RequestMapping(value = {"/css/*", "*.css", "/css/**"}) // TODO which mapping is working?
    public void getCssFile(HttpServletRequest request, HttpServletResponse response) {        
        String url = request.getRequestURI().toString();
        url = url.replaceFirst("/spring/css", "");
        System.out.println("searching for css with url " + url);
        CSS css = cService.findCSSByName(url);
        if(css == null) {
            System.out.println("Css " + url + " not found.");
            // TODO not found
            return;
        }
        try {
            if(response.getOutputStream() == null) {
                System.out.println("CssController.getCssFile: response.outputStream is null!");
                return;
            }
            response.getOutputStream().print(css.getCss());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value = "/cms/styles", method = RequestMethod.GET)
    public ModelAndView styles(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/styles");
        List<CSS> sl = cService.getAllStyles();
        return new ModelAndView("cms/style/styles", "styles", sl);
    }
    
    @RequestMapping(value = "/cms/style/{id}", method = RequestMethod.GET)
    public ModelAndView style(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/style/" + id);
        CSS c = cService.getCSSById(id);
        return new ModelAndView("cms/style/style").addObject("style", c);
    }
    
    @RequestMapping(value = "/cms/style/create", method = RequestMethod.GET)
    public ModelAndView styleCreateGet(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/style/create get");
        return new ModelAndView("cms/style/create");
    }
    
    @RequestMapping(value = "/cms/style/create", method = RequestMethod.POST)
    public String styleCreatePost(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/style/create post");
        try {
            cService.createCSS(request.getParameter("name"),
                    request.getParameter("description"),
                    request.getParameter("css"));
        } catch(Exception e) {            
            sendJson(response, cService.errorToJson(e.getMessage(), 0));
            return null;
        }
        return "redirect:/cms/styles";
    }    

}
